﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using QLTB_CTK34.Tinh;
using BLL;
using Entities;
using System.Text.RegularExpressions;

namespace QLTB_CTK34
{
    public partial class FrmQuanLiThietBi : Form
    {
        public FrmQuanLiThietBi()
        {
            InitializeComponent();
            dgvThietBi.DataSource = QLThietBi.GetAll();
            cbbDieuKien.SelectedIndex = 0;
        }
        public FrmQuanLiThietBi(string LoaiTB)
            : this()
        {
            dgvThietBi.DataSource = QLThietBi.FindByCondition(x => Regex.IsMatch(x.LoaiTB.Trim().ToLower(), @LoaiTB));
            cbbDieuKien.SelectedIndex = 0;
        }
        /// <summary>
        /// bắt sự kiện clik lên datagridview.
        /// </summary>
        string maTB;
        public ThietBi ThietBiChon;
        private void drThietBi_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            GetMaTB(e);
            row = e.RowIndex;
            if (e.RowIndex == -1 && e.ColumnIndex > -1)
            {
                QLThietBi.XapXep(dgvThietBi, e);
            }
            ChọnToolStripMenuItem.Visible = dgvThietBi.SelectedRows.Count > 0;
            XemLỗithiếtbịToolStripMenuItem.Visible = QLTBLoiThietBi.FindByMaTB(GetMaTBTheoDataGridViewSeletced()) != null;
        }
        int row = 0;
        void GetMaTB(DataGridViewCellEventArgs e)
        {
            maTB = e.RowIndex != -1 ? dgvThietBi.Rows[e.RowIndex].Cells["MaThietBi"].Value.ToString() : "";
        }
        void LoadDataGridview()
        {
            dgvThietBi.DataSource = QLThietBi.GetAll();
        }
        /// <summary>
        /// load form thiết bị.
        /// </summary>
        private void FormThietBi_Load(object sender, EventArgs e)
        {
            dgvThietBi.Rows[0].Selected = true;
            LoadPhanQuyen();
        }
        /// <summary>
        /// các toolstrip hổ trợ sự kiện thêm,xóa ,sửa.
        /// </summary>
        private void toolStripButtonThêm_Click(object sender, EventArgs e)
        {
            FormThemThietBi ttb = new FormThemThietBi();
            if (ttb.ShowDialog(this) == DialogResult.OK)
            {
                Generic.ReloadDataGridView<ThietBi>(dgvThietBi);
                Generic.SelectDataGridviewRowTheoMa(dgvThietBi, "MaThietBi", ttb.tb.MaTB);
            }
        }
        ThietBi GetThietBiSeletced()
        {
            return QLThietBi.FindByID(GetMaTBTheoDataGridViewSeletced());
        }
        private void toolStripButtonXoa_Click(object sender, EventArgs e)
        {
            for (int i = dgvThietBi.SelectedRows.Count - 1; i >= 0; i--)
                QLThietBi.DeleteByMaTB(dgvThietBi.SelectedRows[i].Cells["MaThietBi"].Value.ToString());
            LoadDataGridview();
			QLNhatKy.ThemNhatKy("xóa thiết bị");
        }
        private void toolStripButtonSua_Click(object sender, EventArgs e)
        {
            if (dgvThietBi.SelectedRows.Count > 0)
            {
                string matbsua = dgvThietBi.SelectedRows[0].Cells["MaThietBi"].Value.ToString();
                ThietBi tbsua = QLThietBi.FindByCondition(x => x.MaTB == matbsua).First();
                FormCapNhapThietBi cntb = new FormCapNhapThietBi(tbsua);
                if (cntb.ShowDialog() == DialogResult.OK)
                {
                    Generic.ReloadDataGridView<ThietBi>(dgvThietBi);
                    Generic.SelectDataGridviewRowTheoMa(dgvThietBi, "MaThietBi", cntb.tbsua.MaTB);
                }
            }
        }
        /// <summary>
        /// click đúp vào dòng chọn sẽ hiện form cập nhập thiết bị.
        /// </summary>
        private void drThietBi_DoubleClick(object sender, EventArgs e)
        {
            if (!ChọnToolStripMenuItem.Visible)
            {
                toolStripButtonSua.PerformClick();
            }
            else
            {
                ChọnToolStripMenuItem.PerformClick();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            LoadDataGridview();
        }
        /// <summary>
        /// hàm tìm kiếm dữ liệu.
        /// </summary>
        private void txtTTTimKiem_TextChanged_1(object sender, EventArgs e)
        {
            QLThietBi.DKTimKiem dk = (QLThietBi.DKTimKiem)cbbDieuKien.SelectedIndex;
            QLThietBi.TimKiem(dgvThietBi, dk, txtTTTimKiem.Text);
        }

        private void chọnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ThietBiChon = QLThietBi.FindByID(dgvThietBi.SelectedRows[0].Cells["MaThietBi"].Value.ToString());
            DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// click vao toolstrip in ấn hiện form in .
        /// </summary>
        private void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
            FrmInAn inan = new FrmInAn(DKForm.thietbi);
            inan.ShowDialog();
        }
        string GetMaTBTheoDataGridViewSeletced()
        {
            return dgvThietBi.SelectedRows[0].Cells["MaThietBi"].Value.ToString();
        }
        private void chọnToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            ThietBiChon = QLThietBi.FindByID(GetMaTBTheoDataGridViewSeletced());
            DialogResult = DialogResult.OK;
        }

        private void xemLỗithiếtbịToolStripMenuItem_Click(object sender, EventArgs e)
        {
          var tbltb=QLTBLoiThietBi.FindByMaTB(GetMaTBTheoDataGridViewSeletced());
          FrmCapNhatLTB frmcnltb = new FrmCapNhatLTB(tbltb.MaLoiTB );
          frmcnltb.ShowDialog();
        }
        void LoadPhanQuyen()
        {
            toolStripButtonThêm.Visible = thêmToolStripMenuItem.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "ThemTB");
            toolStripButtonSua.Visible = cậpNhậtToolStripMenuItem.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "SuaTB");
            toolStripButtonXoa.Visible = xóaToolStripMenuItem.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "XoaTB");
            toolStripButtonPrint.Visible = PhanQuyen.CheckQuyenSuDung(MyAccount.account.MaTK, "InTB");
        }
        /// <summary>
        /// click vao toolstrip thoát, thoát form.
        /// </summary>
        private void toolStripButtonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

		private void dgvThietBi_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				var a = dgvThietBi.HitTest(e.X, e.Y);
				dgvThietBi.ClearSelection();
				dgvThietBi.Rows[a.RowIndex].Selected = true;
			}
		}
    }
}
